package com.situ.springboot.controller;

import com.situ.springboot.pojo.BlogType;
import com.situ.springboot.pojo.query.BlogTypeQuery;
import com.situ.springboot.service.IBlogTypeService;
import com.situ.springboot.util.PageResult;
import com.situ.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/blogType")
public class BlogTypeController {
    //注入对象使用
    //@Autowired
    //private BlogTypeMapper blogTypeMapper;
    @Autowired
    private IBlogTypeService blogTypeService;

    // /blogType/selectAll
    @RequestMapping("/selectAll")
    @ResponseBody   //以json形式返回数据
    public Result selectAll() {
        System.out.println("BlogTypeController.selectAll");
        List<BlogType> list = blogTypeService.selectAll();
        return Result.ok(list);
    }

    // /blogType/list?page=1&limit=10&name=zhangsan&email=233@qq.com
    @RequestMapping("/list")
    @ResponseBody   //以json形式返回数据
    public PageResult list(BlogTypeQuery blogTypeQuery) {
        System.out.println("BlogTypeController.list");
        PageResult pageResult = blogTypeService.list(blogTypeQuery);
        return pageResult;
    }

    //转发页面，返回String，不需要加@ResponseBody
    @RequestMapping("/toBlogTypeList")
    public String toBlogTypeList() {
        return "blog_type_list";
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        blogTypeService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        //Alt+Enter
        blogTypeService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/toBlogTypeAdd")
    public String toBlogTypeAdd() {
        return "blog_type_add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(BlogType blogType) {
        blogTypeService.add(blogType);
        return Result.ok("添加成功");
    }

    @RequestMapping("/toBlogTypeUpdate")
    public String toBlogTypeUpdate() {
        return "blog_type_update";
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(Integer id) {
        BlogType blogType = blogTypeService.selectById(id);
        return Result.ok(blogType);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(BlogType blogType) {
        blogTypeService.update(blogType);
        return Result.ok("修改成功");
    }
}
